package com.xawl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xawl.dao.NavMapper;
import com.xawl.pojo.PopularNav;
import com.xawl.service.NavService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Zoom
 * @implNote  热门导航接口
 */

@Controller
@RequestMapping("/nav")
public class NavController {

    @Autowired
    @Qualifier("navServiceImpl")
    protected NavService navService;

    @Autowired
    protected NavMapper navMapper;

    /* 获取数据列表 */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.GET)
    public JSONObject getNavList(int page,int limit){
        PageHelper.startPage(page,limit);
        List<PopularNav> popularNavs = navService.queryAll();
        Integer total = navService.getTotal();
        PageInfo<PopularNav> pageInfo = new PageInfo<>(popularNavs, limit);
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("msg",1);
        json.put("count",total);
        json.put("data",popularNavs);
        return json;
    }

    /* 增加 */
    @RequestMapping("/add")
    public String addNav(){
        return "red-nav-manage/red-nav-manage-edit";
    }

    @RequestMapping("/addData")
    @ResponseBody
    public String addNav(@RequestBody PopularNav popularNav){
        int i = navService.addNav(popularNav);
        return i != 0 ? "ok" : "error";
    }

    /* 编辑 */
    @RequestMapping("/edit/{id}")
    public String editUser(Model model,@PathVariable("id") int id){
        model.addAttribute("pnId",id);
        return "red-nav-manage/red-nav-manage-edit";
    }

    /* 删除 */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteNav(@RequestBody int id){
        navService.deleteNav(id);
        return "ok";
    }

    /* 删除多条记录 */
    @RequestMapping("/deletes")
    @ResponseBody
    public String deleteNavs(@RequestBody Long[]  ids){
        int i = navService.deleteNavs(ids);
        System.out.println(i);
        return "ok";
    }

    /* 更新 */
    @RequestMapping("/update")
    @ResponseBody
    public String updateNav(@RequestBody PopularNav popularNav){
        int i = navService.updateNav(popularNav);
        return i!=0 ? "ok" : "error";
    }

    /* 查看 */
    @RequestMapping("/view/{id}")
    public String viewNav(Model model,@PathVariable("id") int id){
        model.addAttribute("pnId",id);
        return "red-nav-manage/red-nav-manage-view";
    }

    /* 数据回显 */
    @RequestMapping("/viewData")
    @ResponseBody
    public PopularNav viewData(@RequestBody int id){
        PopularNav nav = navService.findNavById(id);
        return nav;
    }

}
